package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.VideoGroupInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.VideoRelationInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.IVideoGroupInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IVideoRelationInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IVideoGroupInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;


@Service
@Transactional
@SuppressWarnings("rawtypes")
public class VideoGroupInfoService implements IVideoGroupInfoService{

	@Autowired
	private IVideoGroupInfoMapper videoGroupInfoMapper;
	
	@Autowired
	private IVideoRelationInfoMapper videoRelationInfoMapper;
	
	
	@Autowired
	private IPubLogMapper pubLogMapper;
	
	/**
	 * 获取视频分组树接口List
	 */
	@Override
	public List<VideoGroupInfo> getVideoGroupInfoList(String videoGroupId, String parentCode,String key,String devAreaCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(null, "0",key,devAreaCode);
			List<VideoGroupInfo> videoGroupInfoList = videoGroupInfoMapper.getVideoGroupInfoList(pramT);//查父分组；
			if(videoGroupInfoList.size()>0){
				for(int i = 0;i<videoGroupInfoList.size();i++){
					String groupId = videoGroupInfoList.get(i).getVideoGroupId();
					Map<String, Object> pramT1 = this.getSelectMap(groupId, null,key,devAreaCode);//查询父分组下的子分组；
					Map<String, Object> pramT2 = this.getRelationMap(groupId,key);//查父分组下的子分组；
					List<VideoGroupInfo> listSonGroup = videoGroupInfoMapper.getVideoGroupInfoList(pramT2);
					List<VideoRelationInfo> listRelationSon = videoRelationInfoMapper.getIVideoRelationInfoList(pramT1);//查父分组下的关联点位；
					for(int j = 0;j<listSonGroup.size();j++){
						String relationId = listSonGroup.get(j).getVideoGroupId();
						Map<String, Object> pramT3 = this.getSelectMap(relationId,null,key,devAreaCode);
						List<VideoRelationInfo> videoRelationInfoList = videoRelationInfoMapper.getIVideoRelationInfoList(pramT3);
						listSonGroup.get(j).setVideoRelationInfoList(videoRelationInfoList);
					}
					videoGroupInfoList.get(i).setVideoGroupInfoList(listSonGroup);
					videoGroupInfoList.get(i).setVideoRelationInfoList(listRelationSon);
				}			
			}
			return videoGroupInfoList;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	/**
	 * 单获取分组分页；
	 */
	@Override
	public Page getVideoGroupInfoPage(String videoGroupId, String parentCode,String key,String devAreaCode,Page page) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(videoGroupId, parentCode,key,devAreaCode);
			page.setMapBean(pramT);
			return videoGroupInfoMapper.getVideoGroupInfoPage(page);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	/**
	 * 根据分组编号获取分组对象信息；
	 */
	@Override
	public VideoGroupInfo getVideoGroupInfo(String videoGroupId) {
		// TODO Auto-generated method stub
		try {
			return videoGroupInfoMapper.selectById(videoGroupId);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
		
	}

	/**
	 * 添加视频分组；
	 */
	@Override
	public ResultPojo insertVideoGroupInfo(VideoGroupInfo videoGroupInfo,String userName) {
		// TODO Auto-generated method stub
		try {
			VideoGroupInfo insertVideoGroupInfo = this.getInsertVideoGroupInfo(videoGroupInfo);
			this.videoGroupInfoMapper.insertSelective(insertVideoGroupInfo);
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
					String.format("添加视频分组编号:{%s} 操作成功!", new Object[] {insertVideoGroupInfo.getVideoGroupId()}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return ResultHelper.getResult(insertVideoGroupInfo.getVideoGroupId());			
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	/**
	 * 更新视频分组；
	 */
	@Override
	public ResultPojo updateVideoGroupInfo(VideoGroupInfo videoGroupInfo,String userName) {
		// TODO Auto-generated method stub
		try {
			videoGroupInfo.setUpdateTime(TimeHelper.dateToLongString());
			//this.videoGroupInfoMapper.updateById(videoGroupInfo);
			this.videoGroupInfoMapper.updateByIdSelective(videoGroupInfo);
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
					String.format("修改视频分组编号:{%s} 操作成功!", new Object[] {videoGroupInfo.getVideoGroupId()}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return ResultHelper.getResult(videoGroupInfo.getVideoGroupId());			
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	/**
	 * 删除视频分组以及级联分组；
	 */
	@Override
	public ResultPojo deleteVideoGroupInfo(String videoGroupId,String userName) {
		// TODO Auto-generated method stub
		try {
			this.videoGroupInfoMapper.deleteById(videoGroupId);//删除本组信息；
			boolean flag = this.isExesitVideoList(videoGroupId);//判断是否有子分组；
			if(flag){
				this.videoGroupInfoMapper.delVideoGroupInfoByParCode(videoGroupId);//删除子分子信息；
				Map<String, Object> hashMap = new HashMap<>();
				hashMap.put("videoGroupId", videoGroupId);
				List<VideoGroupInfo> VideoGroupInfoList = this.videoGroupInfoMapper.getVideoGroupInfoList(hashMap);
				for(int i = 0;i<VideoGroupInfoList.size();i++){
					this.videoGroupInfoMapper.delVideoGroupInfoByParCode(VideoGroupInfoList.get(i).getVideoGroupId());
				}
			}
			int num = this.videoRelationInfoMapper.deleteVideoRelationInfoByGroupId(videoGroupId);//删除关联设备；
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
					String.format("删除关联组编号:{%s} 操作成功!", new Object[] {videoGroupId}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return ResultHelper.getResult(videoGroupId);			
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	
	//---------------------------私有方法开始---------------------------------------//
	private Map<String, Object> getSelectMap(String videoGroupId, String parentCode,String key,String devAreaCode){
		Map<String, Object> map = new HashMap<>();
		if(videoGroupId!=null&&!videoGroupId.equals("")){
			map.put("videoGroupId", videoGroupId);
		}
		if(parentCode!=null&&!parentCode.equals("")){
			map.put("parentCode", parentCode);
		}
		map.put("key", key);
		map.put("devAreaCode", devAreaCode);
		return map;
	}
	
	private VideoGroupInfo getInsertVideoGroupInfo(VideoGroupInfo videoGroupInfo){
		String parendtId = videoGroupInfo.getParentCode();
		if(parendtId==null||"".equals(parendtId)){
			videoGroupInfo.setParentCode("0");
		}
		String groupId = "V" + TimeHelper.getUniqueString();
		String creatTime = TimeHelper.dateToLongString();
		videoGroupInfo.setCreatTime(creatTime);
		videoGroupInfo.setUpdateTime(creatTime);
		videoGroupInfo.setVideoGroupId(groupId);
		return videoGroupInfo;
	}
	
	private boolean isExesitVideoList(String videoGroupId){
		Map<String, Object> map = new HashMap<>();
		map.put("parentCode", videoGroupId);
		List<VideoGroupInfo> list = this.videoGroupInfoMapper.getVideoGroupInfoList(map);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	private Map<String, Object> getRelationMap(String videoGroupId,String key){
		Map<String, Object> map = new HashMap<>();
		map.put("parentCode", videoGroupId);
		map.put("key", key);
		return map;
	}

}
